package org.jeecg.trace.controller;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.trace.dto.BarCodeTraceDTO;
import org.jeecg.trace.dto.MaterialHistoryDTO;
import org.jeecg.trace.dto.QisDefectsDTO;
import org.jeecg.trace.entity.*;
import org.jeecg.trace.service.BarCodeTraceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Api(tags = "生产历史追溯")
@RestController
@RequestMapping("/trace/barcodeTrace")
@Slf4j
public class BarCodeTraceController {

    @Autowired
    private BarCodeTraceService barCodeTraceService;

    /**
     * 条码追溯
     *
     */
    //@AutoLog(value = "序列号列表-分页列表查询")
    @ApiOperation(value = "序列号列表-分页列表查询", notes = "序列号列表-分页列表查询")
    @GetMapping(value = "/queryByCode")
    @DS("history")
    public Result<IPage<?>> queryByCode(@RequestParam("unitsn") String unitsn,
                                                      @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                      @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                               HttpServletRequest req) {
        Page<BarCodeTraceDTO> page = new Page<>(pageNo, pageSize);
        IPage<?> product = barCodeTraceService.queryByCode(page,unitsn);
        return Result.OK(product);
    }

    /**
     * 条码追溯生产历史
     *
     * @param unitsn
     * @return
     */
    //@AutoLog(value = "序列号列表-分页列表查询")
    @ApiOperation(value = "序列号列表-分页列表查询", notes = "序列号列表-分页列表查询")
    @GetMapping(value = "/listMesUnitHistoryByUnitsn")
    @DS("history")
    public Result<IPage<MesUnitHistory>> listMesUnitHistoryByUnitsn(@RequestParam(name = "unitsn",required = true) String unitsn,
                                                                    @RequestParam(name = "line",required = true) String line,
                                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                   HttpServletRequest req) {
        Page<MesUnitHistory> page = new Page<>(pageNo, pageSize);
        IPage<MesUnitHistory> historyList = barCodeTraceService.listMesUnitHistoryByUnitsn(page,unitsn,line);
        return Result.OK(historyList);
    }

    /**
     * 查询字条码
     *
     * @param unitsn
     * @return
     */
    //@AutoLog(value = "序列号列表-分页列表查询")
    @ApiOperation(value = "序列号列表-分页列表查询", notes = "序列号列表-分页列表查询")
    @GetMapping(value = "/listMesUnitIdByUnitsn")
    @DS("history")
    public Result<IPage<MesUnitid>> listMesUnitIdByUnitsn(@RequestParam(name = "unitsn",required = true) String unitsn,
                                                          @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                          HttpServletRequest req) {
        Page<MesUnitid> page = new Page<>(pageNo, pageSize);
        IPage<MesUnitid> historyList = barCodeTraceService.listMesUnitIdByUnitsn(page,unitsn);
        return Result.OK(historyList);
    }




    /**
     * 加工参数值列表查询
     *
     * @param mainId
     * @return
     */
    //@AutoLog(value = "序列号列表-分页列表查询")
    @ApiOperation(value = "加工参数值列表查询", notes = "加工参数值列表查询")
    @GetMapping(value = "/listMesProcDataValueByMainId")
    @DS("history")
    public Result<IPage<MesProcDataValue>> listMesProcDataValueByMainId(@RequestParam(name = "mainId",required = true) String mainId,
                                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "999") Integer pageSize,
                                                                       HttpServletRequest req) {
        Page<MesProcDataValue> page = new Page<>(pageNo, 999);
        IPage<MesProcDataValue> historyList = barCodeTraceService.listMesProcDataValueByMainId(page,mainId);
        return Result.OK(historyList);
    }

    /**
     * 加工参数值列表查询
     *
     * @param unitsn
     * @return
     */
    //@AutoLog(value = "序列号列表-分页列表查询")
    @ApiOperation(value = "加工参数值列表查询", notes = "加工参数值列表查询")
    @GetMapping(value = "/mesMaterialSetByProduct")
    @DS("history")
    public Result<List<MaterialHistoryDTO>> mesMaterialSetByProduct(@RequestParam(name = "unitsn",required = true) String unitsn,
                                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                    @RequestParam(name = "pageSize", defaultValue = "999") Integer pageSize,
                                                                    HttpServletRequest req) {
        Page<MesMaterialSet> page = new Page<>(pageNo, pageSize);
        List<MaterialHistoryDTO> pageList = barCodeTraceService.mesMaterialSetByProduct(page,unitsn);
        return Result.OK(pageList);
    }

    /**
     * 加工参数值列表查询
     *
     * @param unitsn
     * @return
     */
    //@AutoLog(value = "序列号列表-分页列表查询")
    @ApiOperation(value = "加工参数值列表查询", notes = "加工参数值列表查询")
    @GetMapping(value = "/listQisDefectsByMainId")
    @DS("history")
    public Result<List<QisDefectsDTO>> listQisDefectsByMainId(@RequestParam(name = "unitsn",required = true) String unitsn,
                                                              HttpServletRequest req) {
        List<QisDefectsDTO> pageList = barCodeTraceService.listQisDefectsByMainId(unitsn);
        return Result.OK(pageList);
    }

    /**
     * 加工参数值列表查询
     *
     * @param historyId
     * @return
     */
    //@AutoLog(value = "序列号列表-分页列表查询")
    @ApiOperation(value = "加工参数值列表查询", notes = "加工参数值列表查询")
    @GetMapping(value = "/listMesStatusWordValueByMainId")
    @DS("history")
    public Result<List<MesStatusbitCfg>> listMesStatusWordValueByMainId(@RequestParam(name = "historyId",required = true) String historyId,
                                                                        @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                        @RequestParam(name = "pageSize", defaultValue = "999") Integer pageSize,
                                                                        HttpServletRequest req) {
        Page<MesMaterialSet> page = new Page<>(pageNo, pageSize);
        List<MesStatusbitCfg> pageList = barCodeTraceService.listMesStatusWordValueByMainId(page,historyId);
        return Result.OK(pageList);
    }

    /**
     * 导出
     *
     * @return
     */
    @GetMapping (value = "/exportXls")
    @DS("history")
    public void exportXsl(HttpServletRequest request, HttpServletResponse response,BarCodeTraceDTO barCodeTraceDTO) {
        barCodeTraceService.exportXsl(request,response,barCodeTraceDTO);
    }

}
